﻿using Microsoft.WindowsAPICodePack.Dialogs;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        DataTable source_files = new DataTable();
        DataTable target_files = new DataTable();
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            string name = btn.Name;

          

           
           

            switch (name)
            {
                case "btn_source":

                    CommonOpenFileDialog dialog = new CommonOpenFileDialog();
                    //dialog.IsFolderPicker = true;//设置为选择文件
                    dialog.Multiselect = true;
                    if (dialog.ShowDialog() != CommonFileDialogResult.Ok)
                    {
                        return;
                    }
                    source_files = new DataTable();
                    source_files.Columns.Add("name");
                    var env= dialog.FileNames.GetEnumerator();
                    string f_names = "";
                    while (env.MoveNext())
                    {
                        string f_name= env.Current;
                        f_names += f_name + "|";
                        DataRow r = source_files.NewRow();
                        r["name"] = f_name;
                        source_files.Rows.Add(r);
                    }
                    
                    text_source.Text = f_names;
                    Console.WriteLine("source:"+f_names);

                    break;
                case "btn_target":
                    dialog = new CommonOpenFileDialog();
                    dialog.IsFolderPicker = true;//设置为选择文件
                    dialog.Multiselect = true;
                    if (dialog.ShowDialog() != CommonFileDialogResult.Ok)
                    {
                        return;
                    }
                    target_files = new DataTable();
                    target_files.Columns.Add("name");
                    env = dialog.FileNames.GetEnumerator();
                    f_names = "";
                    while (env.MoveNext())
                    {
                        string f_name = env.Current;
                        f_names += f_name + "|";
                        DataRow r = target_files.NewRow();
                        r["name"] = f_name;
                        target_files.Rows.Add(r);
                    }
                    text_target.Text = f_names;
                    
                    Console.WriteLine("taeget:"+f_names);

                    break;
                case "btn_cancel":
                    this.Close();
                    break;
                case "btn_start":
                    if (source_files.Rows.Count==0 || target_files.Rows.Count == 0)
                    {
                        return;
                    }

                    for(int i = 0; i < target_files.Rows.Count; i++)
                    {
                        string target=target_files.Rows[i]["name"].ToString();
                        Console.WriteLine(target);
                        for(int j = 0; j < source_files.Rows.Count; j++)
                        {
                            string source=source_files.Rows[j]["name"].ToString();
                            string  n_source=source.Substring(source.LastIndexOf('\\'));
                            Console.WriteLine("\t-->"+source+"  --> "+n_source+"  target: "+target+"  target file:  "+ target + "\\" + n_source);
                            CopyFile(source, target+"\\"+n_source);
                            
                        }
                        Console.WriteLine("copy complete");
                    }
                    MessageBox.Show("完成");
                    source_files = new DataTable();
                    target_files = new DataTable();

                    text_source.Text = "";
                    text_target.Text = "";

                    break;
            }

        }


        /// <summary>
        /// 复制文件
        /// </summary>
        /// <param name="sourcePath"></param>
        /// <param name="destPath"></param>
        private void CopyFile(string sourcePath, string destPath)
        {
            FileInfo f = new FileInfo(sourcePath);
            FileStream fsR = f.OpenRead();
            try {
                FileStream fsW = File.Open(destPath, FileMode.OpenOrCreate);
                long fileLength = f.Length;
                byte[] buffer = new byte[1024];
                int n = 0;

                while (true)
                {
                    //读写文件
                    n = fsR.Read(buffer, 0, 1024);
                    if (n == 0)
                    {
                        break;
                    }
                    fsW.Write(buffer, 0, n);
                    fsW.Flush();

                }
                fsR.Close();
                fsW.Close();
            }
            catch
            {

            }
            }
  


    }
}
